﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectThuCung.Models;

namespace ProjectThuCung.Controllers
{
    public class SanPhamController : Controller
    {
        //
        // GET: /SanPham/
        ThuCungDB db = new ThuCungDB();

        public ActionResult ThuCung(string loaisanpham, string phanloai, string trang, string Timkiem, string Gia, string Xuat_xu)
        {
            if (Timkiem != null)
                return RedirectToAction("TimSanPham", "SanPham", new { timkiem = Timkiem, gia = Gia, xuatxu = Xuat_xu });
            if (loaisanpham == null || phanloai == null)
            {
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThuCung
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;
                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThuCung
                                        select d).Skip(0).Take(20).ToList();

                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThuCung
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThuCung
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThuCung
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            else
            {
                int ss = int.Parse(phanloai);
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPhamID == ss
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;

                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip(0).Take(20).ToList();
                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPhamID == ss
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPhamID == ss
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            return View();
        }

        public ActionResult Chuong(string loaisanpham, string phanloai, string trang, string Timkiem, string Gia, string Xuat_xu)
        {
            if (Timkiem != null)
                return RedirectToAction("TimSanPham", "SanPham", new { timkiem = Timkiem, gia = Gia, xuatxu = Xuat_xu });
            if (loaisanpham == null || phanloai == null)
            {
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Chuong
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;
                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Chuong
                                        select d).Skip(0).Take(20).ToList();

                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Chuong
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Chuong
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Chuong
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            else
            {
                int ss = int.Parse(phanloai);
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPhamID == ss
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;

                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip(0).Take(20).ToList();
                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPhamID == ss
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPhamID == ss
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            return View();
        }

        public ActionResult Thuoc(string loaisanpham, string phanloai, string trang, string Timkiem, string Gia, string Xuat_xu)
        {
            if (Timkiem != null)
                return RedirectToAction("TimSanPham", "SanPham", new { timkiem = Timkiem, gia = Gia, xuatxu = Xuat_xu });
            if (loaisanpham == null || phanloai == null)
            {
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Thuoc
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;
                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Thuoc
                                        select d).Skip(0).Take(20).ToList();

                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Thuoc
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Thuoc
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.Thuoc
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            else
            {
                int ss = int.Parse(phanloai);
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPhamID == ss
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;

                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip(0).Take(20).ToList();
                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPhamID == ss
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPhamID == ss
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            return View();
        }

        public ActionResult ThucAn(string loaisanpham, string phanloai, string trang, string Timkiem, string Gia, string Xuat_xu)
        {
            if (Timkiem != null)
                return RedirectToAction("TimSanPham", "SanPham", new { timkiem = Timkiem, gia = Gia, xuatxu = Xuat_xu });
            if (loaisanpham == null || phanloai == null)
            {
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThucAn
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;
                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThucAn
                                        select d).Skip(0).Take(20).ToList();

                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThucAn
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThucAn
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThucAn
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            else
            {
                int ss = int.Parse(phanloai);
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPhamID == ss
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;

                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip(0).Take(20).ToList();
                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPhamID == ss
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPhamID == ss
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            return View();
        }

        public ActionResult DoChoi(string loaisanpham, string phanloai, string trang, string Timkiem, string Gia, string Xuat_xu)
        {
            if (Timkiem != null)
                return RedirectToAction("TimSanPham", "SanPham", new { timkiem = Timkiem, gia = Gia, xuatxu = Xuat_xu });
            if (loaisanpham == null || phanloai == null)
            {
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.DoChoi
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;
                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.DoChoi
                                        select d).Skip(0).Take(20).ToList();

                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.DoChoi
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.DoChoi
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.DoChoi
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            else
            {
                int ss = int.Parse(phanloai);
                int xx = (from d in db.SanPhams
                          orderby d.SanPhamID descending
                          where d.LoaiSanPhamID == ss
                          select d).Count();
                if (xx % 20 == 0)
                {
                    xx = xx / 20;
                }
                else
                {
                    xx = xx / 20 + 1;
                }
                ViewBag.count = xx;
                ViewBag.loaisanpham = loaisanpham;
                ViewBag.phanloai = phanloai;
                ViewBag.trang = trang;

                if (trang != null)
                {
                    if (int.Parse(trang) <= 0)
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip(0).Take(20).ToList();
                    }
                    else if (int.Parse(trang) >= ViewBag.count)
                    {
                        if (ViewBag.count != 0)
                        {
                            ViewBag.data = (from d in db.SanPhams
                                            orderby d.SanPhamID descending
                                            where d.LoaiSanPhamID == ss
                                            select d).Skip((xx - 1) * 20).Take(20).ToList();
                        }
                        else
                        {
                            ViewBag.data = null;
                        }
                    }
                    else
                    {
                        ViewBag.data = (from d in db.SanPhams
                                        orderby d.SanPhamID descending
                                        where d.LoaiSanPhamID == ss
                                        select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                    }
                }
                else
                {
                    ViewBag.data = (from d in db.SanPhams
                                    orderby d.SanPhamID descending
                                    where d.LoaiSanPhamID == ss
                                    select d).Skip(0).Take(20).ToList();
                }
            }
            return View();
        }

        public ActionResult Index()
        {
            return RedirectToAction("ThuCung");
        }
        public ActionResult ThongTinSanPham(int id, string Timkiem, string Gia, string Xuat_xu)
        {
            if (Timkiem != null)
                return RedirectToAction("TimSanPham", "SanPham", new { timkiem = Timkiem, gia = Gia, xuatxu = Xuat_xu });
            ViewBag.data = (from d in db.SanPhams
                            where d.SanPhamID == id
                            select d).Take(20).ToList();

            ViewBag.quantam = (from d in db.SanPhams
                               orderby d.SanPhamID descending
                               where d.SanPhamID != id
                               select d).Take(4).ToList();
            foreach(var data in(List<SanPham>)ViewBag.quantam)
            {
                ViewBag.lienquan = (from d in db.SanPhams
                      orderby d.SanPhamID descending
                      where d.LoaiSanPhamID == data.LoaiSanPhamID 
                      select d).Take(4).ToList();
            }
            return View();
        }
        public ActionResult TimSanPham(string timkiem, string gia, string xuatxu, string Timkiem, string Gia, string Xuat_xu, string trang)
        {
            ViewBag.timkiem = timkiem;
            ViewBag.gia = gia;
            ViewBag.xuatxu = xuatxu;
            ViewBag.trang = trang;
            if (Timkiem != null && Gia != null && Xuat_xu != null)
            {
                return RedirectToAction("TimSanPham", "SanPham", new { timkiem = Timkiem, gia = Gia, xuatxu = Xuat_xu });
            }
            else
            {
                switch (gia)
                {
                    case null:
                    case "0":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                            orderby d.Gia descending
                                            where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true
                                            select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;
                    case "1":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia <= 100
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia <= 100
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia <= 100
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia <= 100
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia <= 100
                                                select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia <= 100
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia <= 100
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia <= 100
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia <= 100
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia <= 100
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;
                    case "2":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 100 && d.Gia <= 200
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 100 && d.Gia <= 200
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 100 && d.Gia <= 200
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 100 && d.Gia <= 200
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 100 && d.Gia <= 200
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 100 && d.Gia <= 200
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 100 && d.Gia <= 200
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 100 && d.Gia <= 200
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 100 && d.Gia <= 200
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 100 && d.Gia <= 200
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;
                    case "3":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 200 && d.Gia <= 2000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 200 && d.Gia <= 2000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 200 && d.Gia <= 2000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 200 && d.Gia <= 2000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 200 && d.Gia <= 2000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 200 && d.Gia <= 2000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 200 && d.Gia <= 2000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 200 && d.Gia <= 2000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 200 && d.Gia <= 2000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                            orderby d.Gia descending
                                            where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 1000
                                            select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;
                    case "4":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 1000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 1000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 1000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 1000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 1000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 1000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 1000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 1000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 1000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 1000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;
                    case "20":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 1000 && d.Gia <= 2000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 1000 && d.Gia <= 2000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 1000 && d.Gia <= 2000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 1000 && d.Gia <= 2000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 1000 && d.Gia <= 2000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 1000 && d.Gia <= 2000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 1000 && d.Gia <= 2000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 1000 && d.Gia <= 2000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 1000 && d.Gia <= 2000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 1000 && d.Gia <= 2000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;
                    case "6":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 20000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 20000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 20000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 20000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 2000 && d.Gia <= 20000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 20000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 20000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 20000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 20000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 2000 && d.Gia <= 20000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;
                    case "7":
                        if (xuatxu == "0" || xuatxu == null)
                        {
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 20000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 20000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 20000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 20000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.Gia >= 20000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        else
                        {
                            int x_xuatxu_temp = int.Parse(xuatxu);
                            int xx = (from d in db.SanPhams
                                      orderby d.Gia descending
                                      where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 20000
                                      select d).Count();
                            if (xx % 20 == 0)
                            {
                                xx = xx / 20;
                            }
                            else
                            {
                                xx = xx / 20 + 1;
                            }
                            ViewBag.count = xx;

                            if (trang != null)
                            {
                                if (int.Parse(trang) <= 0)
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 20000
                                                    select d).Skip(0).Take(20).ToList();

                                }
                                else if (int.Parse(trang) >= ViewBag.count)
                                {
                                    if (ViewBag.count != 0)
                                    {
                                        ViewBag.data = (from d in db.SanPhams
                                                        orderby d.Gia descending
                                                        where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 20000
                                                        select d).Skip((xx - 1) * 20).Take(20).ToList();
                                    }
                                    else
                                    {
                                        ViewBag.data = null;
                                    }
                                }
                                else
                                {
                                    ViewBag.data = (from d in db.SanPhams
                                                    orderby d.Gia descending
                                                    where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 20000
                                                    select d).Skip((int.Parse(trang) - 1) * 20).Take(20).ToList();
                                }
                            }
                            else
                            {
                                ViewBag.data = (from d in db.SanPhams
                                                orderby d.Gia descending
                                                where (d.TenSanPham.ToLower().Trim()).Contains(timkiem.ToLower().Trim()) == true && d.XuatXuID == x_xuatxu_temp && d.Gia >= 20000
                                                select d).Skip(0).Take(20).ToList();
                            }
                        }
                        break;

                }
            }
            return View();
        }

    }
}
